package jaux.tank.service.system.impl;

import java.util.ArrayList;
import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;

import jaux.tank.bean.constant.cache.Cache;
import jaux.tank.bean.constant.cache.CacheKey;
import jaux.tank.bean.entity.system.Relation;
import jaux.tank.bean.entity.system.Role;
import jaux.tank.bean.vo.node.ZTreeNode;
import jaux.tank.dao.cache.CacheDao;
import jaux.tank.dao.system.RelationRepository;
import jaux.tank.dao.system.RoleRepository;
import jaux.tank.service.system.RoleService;
import jaux.tank.utils.Convert;

/**
 * Created  on 2018/3/25 0025.
 *
 */
@Service
public class RoleServiceImpl implements RoleService {
	
	@Autowired
	CacheDao cacheDao;
    @Autowired
    private RoleRepository roleRepository;
    @Autowired
    private RelationRepository relationRepository;
    @Override
    public List<ZTreeNode> roleTreeList() {
        List list = roleRepository.roleTreeList();
        List<ZTreeNode> treeNodes = new ArrayList<>();
        for(int i=0;i<list.size();i++){
            Object[] arr = (Object[]) list.get(i);
            ZTreeNode node = new ZTreeNode();
            node.setId(arr[0].toString());
            if(arr[1]!=null) {
            	node.setpId(arr[1].toString());
            }
            node.setName(arr[2].toString());
            node.setOpen(Boolean.valueOf(arr[3].toString()));
            treeNodes.add(node);
        }
        return treeNodes;
    }

    @Override
    public List<ZTreeNode> roleTreeListByRoleId(String[] ids) {
        List list = roleRepository.roleTreeListByRoleId(ids);
        List<ZTreeNode> treeNodes = new ArrayList<>();
        for(int i=0;i<list.size();i++){
            Object[] arr = (Object[]) list.get(i);
            ZTreeNode node = new ZTreeNode();
            node.setId(arr[0].toString());
            if(arr[1]!=null) {
            	node.setpId(arr[1].toString());
            }
            node.setName(arr[2].toString());
            node.setOpen(Boolean.valueOf(arr[3].toString()));
            node.setChecked(Boolean.valueOf(arr[4].toString()));
            treeNodes.add(node);
        }
        return treeNodes;
    }

    @Override
    public void setAuthority(String roleId, String ids) {
        // 删除该角色所有的权限
        relationRepository.deleteByRoleId(roleId);


        // 添加新的权限
        for (String menuID : ids.split(",")) {
            Relation relation = new Relation();
            relation.setRoleid(roleId);
            relation.setMenuid(menuID);
            relationRepository.save(relation);
        }
    }
    
    @Override
    public Role update(@Valid Role role) {
    	removeCache(role.getId());
    	return roleRepository.save(role);
    }

    @Override
    public void delRoleById(String roleId) {
    	removeCache(roleId);
        //删除角色
        roleRepository.deleteById(roleId);

        // 删除该角色所有的权限
        relationRepository.deleteByRoleId(roleId);
        
        
    }

	private void removeCache(String roleId) {
		//删除缓存
        cacheDao.del(k->k.startsWith(CacheKey.ROLES_NAME) && k.indexOf(roleId)>-1);
        cacheDao.del(k->k.startsWith(CacheKey.SINGLE_ROLE_NAME) && k.indexOf(roleId)>-1);
        cacheDao.del(k->k.startsWith(CacheKey.SINGLE_ROLE_TIP) && k.indexOf(roleId)>-1);
	}
}
